package com.gxa.controller;

import com.gxa.common.Constants;
import com.gxa.common.ResponseData;
import com.gxa.pojo.Role;
import com.gxa.service.RoleService;
import io.swagger.annotations.*;
import org.apache.ibatis.jdbc.Null;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @author bq
 * Date: 2020/10/28 12:40
 * Description:
 * Version: V1.0
 */
@Controller
@RequestMapping("role")
@Api(value = "角色信息",tags = "角色信息接口API")
public class RoleController {
    @Autowired
    private ResponseData responseData;
    @Autowired
    private RoleService roleService;

    /**
     * 查询所有角色
     * @return
     */
    @GetMapping("queryRoles")
    @ApiOperation(value = "查询所有角色",notes = "查询所有角色接口",httpMethod = "GET", response = ResponseData.class)
    @ApiResponses({
            @ApiResponse(code=200,message="查询成功"),
            @ApiResponse(code=400,message="查询失败"),
            @ApiResponse(code=500,message="后台服务错误")
    })
    @ResponseBody
    public ResponseData queryRoles(){
        List<Role> roleList=roleService.queryAll();
        if (roleList!=null){
            responseData.setCode(Constants.SUCCESS);
            responseData.setMsg("查询成功");
            responseData.setData(roleList);
        }else {
            responseData.setCode(Constants.FAILED);
            responseData.setMsg("查询失败");
            responseData.setData(null);
        }
        return responseData;
    }

    /**
     * 根据提交信息修改角色
     * @return
     */
    @PostMapping("updateRole")
    @ApiOperation(value = "根据提交信息修改角色",notes = "根据提交信息修改角色接口",httpMethod = "POST", response = ResponseData.class)
    @ApiResponses({
            @ApiResponse(code=200,message="修改成功"),
            @ApiResponse(code=400,message="修改失败"),
            @ApiResponse(code=500,message="后台服务错误")
    })
    @ResponseBody
    public ResponseData updateRole(@RequestBody @ApiParam(name = "role",value = "角色信息",required = true)Role role){
        int result=roleService.update(role);
        if (result>0){
            responseData.setCode(Constants.SUCCESS);
            responseData.setMsg("修改成功");
            responseData.setData(null);
        }else {
            responseData.setCode(Constants.FAILED);
            responseData.setMsg("修改失败");
            responseData.setData(null);
        }
        return responseData;
    }
    /**
     * 根据提交信息添加角色
     * @return
     */
    @PostMapping("addRole")
    @ApiOperation(value = "根据提交信息添加角色",notes = "根据提交信息添加角色接口",httpMethod = "POST", response = ResponseData.class)
    @ApiResponses({
            @ApiResponse(code=200,message="添加成功"),
            @ApiResponse(code=400,message="添加失败"),
            @ApiResponse(code=500,message="后台服务错误")
    })
    @ResponseBody
    public ResponseData addRole(@RequestBody @ApiParam(name = "role",value = "角色信息",required = true)Role role){
        System.out.println(role);
        int roleId=roleService.addRole(role);
        if ((roleId>0)){
            responseData.setCode(Constants.SUCCESS);
            responseData.setMsg("添加成功");
            responseData.setData(roleId);
        }else {
            responseData.setCode(Constants.FAILED);
            responseData.setMsg("添加失败");
            responseData.setData(null);
        }
        return responseData;
    }
    /**
     * 根据角色主键删除角色
     * @return
     */
    @GetMapping("deleteRoleByRoleId")
    @ApiOperation(value = "根据角色主键删除角色",notes = "根据角色主键删除角色接口",httpMethod = "GET", response = ResponseData.class)
    @ApiResponses({
            @ApiResponse(code=200,message="删除成功"),
            @ApiResponse(code=400,message="删除失败"),
            @ApiResponse(code=500,message="后台服务错误")
    })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roleId",value = "主键",paramType = "query",required = true)
    })
    @ResponseBody
    public ResponseData deleteRoleByRoleId(int roleId){
        int result=roleService.deleteById(roleId);
        if (result>0){
            responseData.setCode(Constants.SUCCESS);
            responseData.setMsg("删除成功");
            responseData.setData(null);
        }else {
            responseData.setCode(Constants.FAILED);
            responseData.setMsg("删除失败");
            responseData.setData(null);
        }
        return responseData;
    }
    /**
     * 给角色添加权限
     * @return
     */
    @PostMapping("addRolePower")
    @ApiOperation(value = "给角色添加权限",notes = "给角色添加权限接口",httpMethod = "POST", response = ResponseData.class)
    @ApiResponses({
            @ApiResponse(code=200,message="添加成功"),
            @ApiResponse(code=400,message="添加失败"),
            @ApiResponse(code=500,message="后台服务错误")
    })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roleId",value = "角色id",paramType = "query",required = true),
            @ApiImplicitParam(name = "powerIds",value = "权限菜单id数组",paramType = "query",required = true)
    })
    @ResponseBody
    public ResponseData addRolePower(@RequestBody Role role){
        int result=roleService.addRolePower(role.getRoleId(),role.getPowerMenuList());
        if ((result>0)){
            responseData.setCode(Constants.SUCCESS);
            responseData.setMsg("添加成功");
            responseData.setData(null);
        }else {
            responseData.setCode(Constants.FAILED);
            responseData.setMsg("添加失败");
            responseData.setData(null);
        }
        return responseData;
    }
    /**
     * 查询所有角色姓名
     * @return
     */
    @GetMapping("selectRoleName")
    @ApiOperation(value = "查询所有角色姓名",notes = "查询所有角色姓名接口",httpMethod = "GET", response = ResponseData.class)
    @ApiResponses({
            @ApiResponse(code=200,message="查询成功"),
            @ApiResponse(code=400,message="查询失败"),
            @ApiResponse(code=500,message="后台服务错误")
    })

    @ResponseBody
    public ResponseData selectRoleName(){
        List<String> roleNames=roleService.selectRoleName();
        if ((roleNames!= null)){
            responseData.setCode(Constants.SUCCESS);
            responseData.setMsg("查询成功");
            responseData.setData(roleNames);
        }else {
            responseData.setCode(Constants.FAILED);
            responseData.setMsg("查询失败");
            responseData.setData(null);
        }
        return responseData;
    }
}
